﻿using Common.Contract;
using Common.Contract.Authentication;
using Common.Contract.Authentication.Record;
using Common.Contract.Authentication.Request;
using Common.Contract.Authentication.Response;
using Common.Contract.Paging;
using Common.Contract.SystemConfig.Record;
using Common.Data.Repository.Enum;
using Common.Service;
using Quartz;
using Customer360AutoJob.Core.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Customer360AutoJob.Jobs
{
    public class ReturnResultCreateDaoRequest : JobBase, IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            List<RequestCreateDaoRecord> listRequestNeedSendMail = RetrieveListCreateDaoRequest();
            if (listRequestNeedSendMail.Count > 0)
            {
                string mailSubject = "Khoi tao DAO thanh cong";
                listRequestNeedSendMail.ForEach(x =>
                {
                    List<string> listCC = new List<string>();
                    if (!string.IsNullOrEmpty(x.Pic))
                    {
                        listCC.Add(x.Pic + "@vpbank.com.vn");
                    }
                    EmailContent content = new EmailContent
                    {
                        Subject = mailSubject,
                        Body = string.Format("Dear anh/chị. {0}Yêu cầu CD{5} đã được xử lý, DAO của anh/chị {1} là: {2}. Anh/ chị vui lòng kiểm tra lại trên T24 và báo lại phòng QTDL nếu có trường hợp nào xử lý sai hoặc thiếu. Khi đó, phòng QTDL sẽ liên hệ với IT để kiểm tra lại. {3}Nếu sau khi kiểm tra và đúng với yêu cầu, anh/ chị vui lòng truy cập vào link sau để đóng yêu cầu{4}Trân trọng cảm ơn! ", Environment.NewLine, x.FullName, x.Dao, Environment.NewLine, Environment.NewLine, x.RequestId.ToString()),
                        CC = listCC,
                        To = x.Email
                    };
                    if (SendEmail(content))
                    {
                        x.StatusCreateDaoRequest = (int)StatusRequestCreateDaoEnum.SendEmail;
                    }
                });
                var listNeedUpdateStatus = listRequestNeedSendMail.Where(x => x.StatusCreateDaoRequest == (int)StatusRequestCreateDaoEnum.SendEmail).ToList();
                UpdateStatusAfterSendEmail(listNeedUpdateStatus);
            }
            List<RequestCreateDaoRecord> listRequestRejectedNeedSendMail = RetrieveListCreateDaoRequestRejected();
            if (listRequestRejectedNeedSendMail.Count > 0)
            {
                string mailSubject = "Khoi tao DAO bi tu choi";
                listRequestRejectedNeedSendMail.ForEach(x =>
                {
                    if (!string.IsNullOrEmpty(x.RejectReason))
                    {

                        List<string> listCC = new List<string>();
                        if (!string.IsNullOrEmpty(x.Pic))
                        {
                            listCC.Add(x.Pic + "@vpbank.com.vn");
                        }
                        EmailContent content = new EmailContent
                        {
                            Subject = mailSubject,
                            Body = string.Format("Kính gửi anh/ chị {0}, {1} Hiện tại yêu cầu CD{2} của anh/ chị bị từ chối do {3}.Kính đề nghị anh/ chị thực hiện khởi tạo lại yêu cầu. {4}Trân trọng", x.FullName, Environment.NewLine, x.RequestId.ToString(), x.RejectReason, Environment.NewLine),
                            CC = listCC,
                            To = x.Email
                        };
                        if (SendEmail(content))
                        {
                            x.StatusCreateDaoRequest = (int)StatusRequestCreateDaoEnum.SendRejectEmail;
                        }
                    }
                });
                var listNeedUpdateStatus = listRequestRejectedNeedSendMail.Where(x => x.StatusCreateDaoRequest == (int)StatusRequestCreateDaoEnum.SendRejectEmail).ToList();
                UpdateStatusAfterSendEmail(listNeedUpdateStatus);
            }
        }

        private List<RequestCreateDaoRecord> RetrieveListCreateDaoRequest()
        {
            RetrieveListRequestCreateDaoByUserResponse result = new RetrieveListRequestCreateDaoByUserResponse();

            FilterRecord filter = new FilterRecord
            {
                CreateDaoRequestStatus = (int)StatusRequestCreateDaoEnum.Done
            };

            UserRecord CurrentUserRecord = new UserRecord
            {
                UserName = "cucnt3",
                GroupIdentifier = "1"
            };
            using (ServiceClient<IAuthentication> serviceClient = new ServiceClient<IAuthentication>())
            {

                RetrieveListRequestCreateDaoByUserRequest request = new RetrieveListRequestCreateDaoByUserRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.DaoManagerController.RetrieveListCreateDaoRequest"
                    },
                    Page = new PageRecord
                    {
                        PageSize = 20,
                        PageIndex = 0
                    },
                    CurrentUser = CurrentUserRecord,
                    Filter = filter
                };
                result = serviceClient.Proxy.RetrieveListRequestCreateDaoByUser(request);
            }
            var listRecord = (result == null || result.Records == null) ? new List<RequestCreateDaoRecord>() : result.Records;
            return listRecord;
        }
        private List<RequestCreateDaoRecord> RetrieveListCreateDaoRequestRejected()
        {
            RetrieveListRequestCreateDaoByUserResponse result = new RetrieveListRequestCreateDaoByUserResponse();

            FilterRecord filter = new FilterRecord
            {
                CreateDaoRequestStatus = (int)StatusRequestCreateDaoEnum.Reject
            };

            UserRecord CurrentUserRecord = new UserRecord
            {
                UserName = "cucnt3",
                GroupIdentifier = "1"
            };
            using (ServiceClient<IAuthentication> serviceClient = new ServiceClient<IAuthentication>())
            {

                RetrieveListRequestCreateDaoByUserRequest request = new RetrieveListRequestCreateDaoByUserRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.DaoManagerController.RetrieveListCreateDaoRequest"
                    },
                    Page = new PageRecord
                    {
                        PageSize = 20,
                        PageIndex = 0
                    },
                    CurrentUser = CurrentUserRecord,
                    Filter = filter
                };
                result = serviceClient.Proxy.RetrieveListRequestCreateDaoByUser(request);
            }
            var listRecord = (result == null || result.Records == null) ? new List<RequestCreateDaoRecord>() : result.Records;
            return listRecord;
        }
    }
}